package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"soul/api/request"
	"soul/inter/dao"
	"soul/inter/model"
)

func DynamicCreate(c *gin.Context) {
	var req request.DynamicCreate
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	var dynamic model.Dynamics
	dao.GetOneByFields(&model.Dynamics{}, &dynamic)
	if dynamic.Id != 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "此条动态已存在",
			"data": nil,
		})
		return
	}
	dynamic = model.Dynamics{
		UserId: req.UserId,
		Common: req.Common,
		Like:   int64(req.Like),
		Look:   req.Look,
	}
	if !dao.Create(&dynamic) {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "动态发布失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "ok",
		"data": dynamic,
	})
}

func DynamicFind(c *gin.Context) {
	var dynamic []model.Dynamics
	dao.GetListByFields(&model.Dynamics{}, &dynamic)
	var dynamics []*model.Dynamics
	for _, v := range dynamic {
		dynamics = append(dynamics, &v)
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "ok",
		"data": dynamics,
	})
}
